package com.bird.pro.ctrl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bird.pro.Page;
import com.bird.pro.Params;
import com.bird.pro.pojo.BirdAdmin;
import com.bird.pro.pojo.BirdPost;
import com.bird.pro.pojo.BirdPosttype;
import com.bird.pro.service.IPostService;
import com.bird.pro.service.IPostTypeService;
import com.bird.pro.vo.PostVo;

@Controller
@RequestMapping("post")
public class PostController {

	@Autowired
	private IPostService postSerive;
	@Autowired
	private IPostTypeService postTypeService;
	
	@RequestMapping("/main")
	public String main(Model model){
		List<BirdPosttype> posttypes = postTypeService.getFirstTypes();
		model.addAttribute("types", posttypes);
		return "post/main";
	}
	
	@RequestMapping("list")
	public String list(Model model,Params params, HttpSession session){

		BirdAdmin admin = (BirdAdmin) session.getAttribute("admin");
		if(!admin.getRoleId().equals(1) && !admin.getRoleId().equals(2)){
			params.setProvince(admin.getAdminProvince());
			params.setCity(admin.getAdminCity());
			params.setCounty(admin.getAdminCounty());
		}
		
		Page page = postSerive.queryPostVos(params);
		model.addAttribute("page", page);
		return "post/list";
	}
	
	@RequestMapping("examine")
	@ResponseBody
	public String examinePost(BirdPost post){
		int result = postSerive.updatePost(post);
		if (result > 0) {
			return "success";
		}
		return "";
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public String deletePost(Integer id){
		int result = postSerive.deletePost(id);
		if (result > 0) {
			return "success";
		}
		return "";
	}
	
	@RequestMapping("getPost")
	@ResponseBody
	public PostVo getPost(Integer id){
		PostVo postVo = postSerive.getPostVoById(id);
		return postVo;
	}
}
